import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:travel/components/search/attractions.dart';
import 'package:travel/components/search/comprehensive.dart';
import 'package:travel/components/search/travels.dart';
import 'package:travel/utils/colors.dart';
import 'package:travel/utils/styles.dart';
import 'package:travel/utils/tools.dart';
import 'package:travel/screen/flutter_screenutil.dart';

class SearchResultController extends GetxController with GetTickerProviderStateMixin {
  RxString search = ''.obs;

  late TabController tabController;

  RxInt index = 0.obs;

  RxList<String> tabs = RxList(['综合', '游记', '景点']);

  @override
  void onInit() {
    super.onInit();
    tabController = TabController(length: tabs.length, vsync: this);
    if (Get.arguments != null) {
      search.value = Get.arguments['result'];
    }
  }

  @override
  void onClose() {
    tabController.dispose();
    super.onClose();
  }
}

class SearchResultView extends GetView<SearchResultController> {
  const SearchResultView({super.key});

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
          appBar: AppBar(
            automaticallyImplyLeading: true,
            leading: InkWell(
              onTap: () {
                Get.back();
              },
              child: Icon(
                Icons.arrow_back_ios,
                color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                size: 20.w,
              ),
            ),
            leadingWidth: 40.w,
            title: Container(
                height: 38.h,
                padding: EdgeInsets.only(left: 10.w, right: 15.w),
                decoration: BoxDecoration(
                    border: Border.all(
                        color: ColorsUtils.parseColorFromHexString(ColorsUtils.color3478)),
                    borderRadius: BorderRadius.all(const Radius.circular(20).w)),
                child: Row(
                  children: [
                    Expanded(
                        child: TextField(
                      controller: ToolsUtils.editingController(controller.search.value),
                      cursorWidth: 2.0,
                      cursorRadius: const Radius.circular(2),
                      cursorColor: ColorsUtils.parseColorFromHexString(ColorsUtils.color3333),
                      style: StylesUtils.customTextStyle(
                          fontSize: 15.sp, color: ColorsUtils.color3333),
                      decoration: InputDecoration(
                        prefixIcon: Icon(Icons.search_sharp, size: 25.sp),
                        prefixIconColor: Colors.black54,
                        prefixIconConstraints: BoxConstraints.expand(width: 35.w),
                        border: InputBorder.none,
                        focusedBorder: InputBorder.none,
                        hintText: '搜索目的地/景点',
                        contentPadding: EdgeInsets.symmetric(vertical: 12.h),
                        isCollapsed: true,
                        isDense: true,
                        hintStyle: StylesUtils.customTextStyle(
                          color: ColorsUtils.color9999,
                        ),
                      ),
                      onChanged: (String keyword) {
                        controller.search.value = keyword;
                      },
                    )),
                    InkWell(
                      onTap: () {
                        controller.search.value = '';
                      },
                      child: Center(
                        child: SizedBox(
                          width: 18,
                          height: 18,
                          child: CustomPaint(
                            painter: _ClosedIcon(),
                          ),
                        ),
                      ),
                    )
                  ],
                )),
            actions: [
              Container(
                width: 40.w,
              )
            ],
            elevation: 0,
            bottom: PreferredSize(
                preferredSize: Size.fromHeight(40.h),
                child: Container(
                  color: Colors.white,
                  padding: EdgeInsets.only(left: 15.w),
                  alignment: Alignment.centerLeft,
                  child: ToolsUtils.getClientTabBar(controller.tabs, controller.tabController,
                      onTap: (int index) {
                    controller.index.value = index;
                  }),
                )),
            systemOverlayStyle: SystemUiOverlayStyle.dark,
          ),
          body: TabBarView(
            controller: controller.tabController,
            children: const [ComprehensivePage(), TravelsPage(), AttractionsPage()],
          ),
        ));
  }
}

class _ClosedIcon extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Paint paint = Paint()
      ..style = PaintingStyle.fill
      ..isAntiAlias = true
      ..color = ColorsUtils.parseColorFromHexString(ColorsUtils.colorCccc);
    canvas.drawCircle(Offset(size.width / 2, size.height / 2), size.height / 2, paint);
    paint.color = Colors.white;
    paint.strokeWidth = 2.5;
    paint.style = PaintingStyle.stroke;

    canvas.drawLine(Offset(size.width / 4, size.height / 4),
        Offset(size.width * 3 / 4, size.height * 3 / 4), paint);

    canvas.drawLine(Offset(size.width / 4, size.height * 3 / 4),
        Offset(size.width * 3 / 4, size.height / 4), paint);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return this != oldDelegate;
  }
}
